//全局变量
var optionid=0;
var type='notype';
var thispage=0;
var pages;


//局部变量
var type='notype';
readdate(type,'name','count','bs',thispage);
option("option",'name','count','bs',thispage);

// ajks

function readdate(type,name,count,bs,page) {
	$.ajax({
		url: "Mune",
		type: "post",
		dataType: "JSON",
		data: {
            one:type,
            two:name,
            three:count,
            four:bs,
            page:page,
		},
		success: function(data) {
            show(data);
		},
		error: function(err) {
		},
	});
}
function option(type,name,count,bs,page) {
    $.ajax({
        url: "Mune",
        type: "post",
        dataType: "JSON",
        data: {
            one:type,
            two:name,
            three:count,
            four:bs,
            page:page,
        },
        success: function(data) {
            optionshow(data);

        },
        error: function(err) {
        },
    });
}

function optionshow(data){
    document.getElementById("bjselect").innerHTML='';
    document.getElementById("addselect").innerHTML='';
    for( var i=0;i<data.length;i++){
        if(data[i].fATHER===undefined){
            data[i].fATHER=null;
        }
        if(data[i].fATHER===" "){
            data[i].fATHER=null;
        }
        if(data[i].fATHER==null){
            createselectshow(data[i].sON,data[i].sON_SEQ);

            MusenCreateshow(data[i].sON,data[i].sON_SEQ);
        }
    }
}





function readdate2(type,name,count,bs,page) {
    $.ajax({
        url: "Mune2",
        type: "post",
        dataType: "JSON",
        data: {
            one:type,
            two:name,
            three:count,
            four:bs,
            page:page,
        },
        success: function(data) {
            show(data);

        },
        error: function(err) {
        },
    });
}
//ajks2
function readdate1(type,name,count,bs,index,optionid,page) {
	$.ajax({
		url: "Mune1",
		type: "post",
		dataType: "JSON",
		data: {
            one:type,
            two:name,
            three:count,
            four:bs,
            five:index,
            six:optionid,
            page:page,
		},
		success: function(data) {
            muneshow(data);
		},
		error: function(err) {
		},
	});
}
//展示
function show(data){
    var fathername="";
    document.getElementById("tbody").innerHTML='';
    document.getElementById("bjselect").innerHTML='';
    option("option",'name','count','bs',thispage);
    pages=data.pages;
    for(i=0;i<data.list.length;i++){
        if(data.list[i].fATHER===undefined){
            data.list[i].fATHER=null;
        }
        createtable(i,data.list[i].sON_SEQ,data.list[i].sON,data.list[i].fATHER,data.list[i].sON_IMG,data.list[i].sON_URL);
    }
}

  
//创建表格
function createtable(i,seq,munename,fathername,img,url){
    var tbody =document.getElementById("tbody");

    var tr =document.createElement("tr");

    var td =document.createElement("td");
    var text =document.createElement("input");
    text.type="text";
    text.value=i;
    td.appendChild(text);

    var td1 =document.createElement("td");
    td1.innerHTML=seq;
    td1.id="seq"+seq;

    var td2=document.createElement("td");
    td2.innerHTML=munename;
    td2.id="td2"+seq;

    var td3=document.createElement("td");
    td3.innerHTML=fathername;

    var td4=document.createElement("td");
    var tb =document.createElement("i");
    i.class=img;
    td4.appendChild(tb);

    var td5=document.createElement("td");
    td5.innerHTML=url;
    td5.id="td5"+seq;

    var td6=document.createElement("td");

    var bj =document.createElement('input');
    bj.type ='button';
    bj.value='编辑';
    bj.onclick =function(){
        document.getElementById("text4").value=document.getElementById(td2.id).innerHTML
        document.getElementById("text2").value =document.getElementById(td5.id).innerHTML;
        optionid=document.getElementById(td1.id).innerHTML;
        $('#myModal').modal('show', 'fit');

        // document.getElementById("update").style="block";

    }

    var del =document.createElement('input');
    del.type ='button';
    del.value='删除';
    del.onclick =function(){
        seq =document.getElementById( td1.id).innerHTML;
        readdate1("del",seq,'count','bs','index','optionid',thispage);
    }
    td6.appendChild(bj);
    td6.appendChild(del);

    tr.appendChild(td);
    tr.appendChild(td1);
    tr.appendChild(td2);
    tr.appendChild(td3);
    tr.appendChild(td4);
    tr.appendChild(td5);
    tr.appendChild(td6);
    tbody.appendChild(tr);
}

function selectshow(){
    document.getElementById("bjselect").innerHTML='';
    for(i=0;i<data.length;i++){

    }
}
// 下拉框1
function createselectshow(name,seq){
    var select =document.getElementById("bjselect");
    optionid=seq;
    var option =document.createElement("option");
    option.innerHTML=name;
    option.value=name;
    select.appendChild(option);

}
//下拉框2
function MusenCreateshow(name,seq){
    var select =document.getElementById("addselect");
    optionid=seq;
    var option =document.createElement("option");
    option.innerHTML=name;
    option.value=seq;
    select.appendChild(option);
}

function add(){
    var name =document.getElementById("text4").value;
    var img =document.getElementById("text1").value;
    var url =document.getElementById("text2").value;
    var kd1 = document.getElementById('bjselect');
	var d1 = kd1.selectedIndex;
    var idnex1 = kd1.options[d1].value;
    readdate1("bj",name,img,url,idnex1,optionid,thispage);
    $('#myModal').modal('hide', 'fit');
}
function Cancle(){
    $('#addmune').modal('hide', 'fit');
    $('#myModal').modal('hide', 'fit');
}
function muneshow(){ var type='notype';
    readdate(type,'name','count','bs',thispage);
    option("option",'name','count','bs',thispage);
}

    function addmune(){
        $('#addmune').modal('show', 'fit');
    }

    function muneadd(){
        var name =document.getElementById("addname").value;
        var img =document.getElementById("addimage").value;
        var url =document.getElementById("addurl").value;
        var kd1 = document.getElementById('addselect');
        var d1 = kd1.selectedIndex;
        var idnex1 = kd1.options[d1].value;
        alert("进入")
        alert("菜单名:"+name+"图片路径:"+img+"连接地址"+url+"菜单值"+idnex1);
        readdate1("add",name,img,url,idnex1,"添加菜单",thispage);
        option("option",'name','count','bs',thispage);
        $('#addmune').modal('hide', 'fit');
    }

function Arcalefron(){
    if(thispage-1>0){
        thispage=(thispage-1);
        readdate("notype",'name','count','bs',thispage);
    }else {
        alert("没有上一页");
    }
}
function Arcalefronnext() {
    if (thispage < pages) {
        thispage = (thispage + 1);
        readdate("notype",'name','count','bs',thispage);
    } else {
        alert("没有下一页");
    }
}